package com.example.freeengine.filetranslate.persistence.repository;

import android.content.Context;

import androidx.lifecycle.LiveData;

import com.example.freeengine.constants.FreeEngineConstant;
import com.example.freeengine.filetranslate.persistence.dao.FileTranslateDownloadFileDao;
import com.example.freeengine.filetranslate.persistence.dao.FileTranslateProgressDao;
import com.example.freeengine.filetranslate.persistence.entity.FileTranslateDownloadFile;
import com.example.freeengine.filetranslate.persistence.entity.FileTranslateProgress;
import com.example.freeengine.filetranslate.persistence.factory.FileTranslateDatabaseFactory;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @author Terry Deng
 */
public class FileTranslateRepository {

    Context context;
    FileTranslateProgressDao fileTranslateProgressDao;
    FileTranslateDownloadFileDao fileTranslateDownloadFileDao;

    public FileTranslateRepository(){}
    public FileTranslateRepository(Context context){
        this.context = context;
        fileTranslateProgressDao = FileTranslateDatabaseFactory.getFileTranslateProgressDatabase(context, FreeEngineConstant.dbName).fileTranslateProgressDao();
        fileTranslateDownloadFileDao = FileTranslateDatabaseFactory.getFileTranslateProgressDatabase(context, FreeEngineConstant.dbName).fileTranslateDownloadFileDao();
    }

    public Long insert(FileTranslateProgress fileTranslateProgress) throws ExecutionException, InterruptedException {
        return fileTranslateProgressDao.insert(fileTranslateProgress).get();
    }
    public LiveData<List<FileTranslateProgress>> findAll(){
        return fileTranslateProgressDao.findAll();
    }
    public Integer update(FileTranslateProgress fileTranslateProgress) throws ExecutionException, InterruptedException {
        return fileTranslateProgressDao.update(fileTranslateProgress).get();
    }
    public Integer delete(FileTranslateProgress fileTranslateProgress) throws ExecutionException, InterruptedException {
        return fileTranslateProgressDao.delete(fileTranslateProgress).get();
    }
    public FileTranslateProgress findById(Long id) throws ExecutionException, InterruptedException {
        return fileTranslateProgressDao.findById(id).get();
    }

    public Long insert(FileTranslateDownloadFile fileTranslateDownloadFile) throws ExecutionException, InterruptedException {
        return fileTranslateDownloadFileDao.insert(fileTranslateDownloadFile).get();
    }
    public LiveData<List<FileTranslateDownloadFile>> findAllDownloadFiles(){
        return fileTranslateDownloadFileDao.findAll();
    }
    public List<FileTranslateDownloadFile> findAllDownloadFilesBlock() throws ExecutionException, InterruptedException, TimeoutException {
        return fileTranslateDownloadFileDao.findAllBlock().get(2, TimeUnit.SECONDS);
    }

    public Integer update(FileTranslateDownloadFile fileTranslateDownloadFile) throws ExecutionException, InterruptedException {
        return fileTranslateDownloadFileDao.update(fileTranslateDownloadFile).get();
    }
    public Integer delete(FileTranslateDownloadFile fileTranslateDownloadFile) throws ExecutionException, InterruptedException {
        return fileTranslateDownloadFileDao.delete(fileTranslateDownloadFile).get();
    }
    public FileTranslateDownloadFile findDownloadFileById(Long id) throws ExecutionException, InterruptedException {
        return fileTranslateDownloadFileDao.findById(id).get();
    }


}
